<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				businessTripTypeCode: '',
				leadInventoryCounterIdCache:'asset-deviceStocktaking-leadInventoryCounterId-cache',
				inventoryTakerIdCache:'asset-deviceStocktaking-inventoryTakerId-cache',
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','salaryInstId','salaryInstName'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			showBtn(options) {
				let _that = this
				_that.$refs.autoform.changeConfigAttr("dataStr", "showButton", true)
				_that.$refs.autoform.changeConfigAttr("dataStr", "buttonText", "新增模板")
				_that.$refs.autoform.changeConfigAttr("dataStr", "buttonLink",
					"/pages/form/asset-deviceStocktaking/deviceStocktakingTemplate?busCode="+options.code)
			},
			clearLeadInventoryCounter(){
				let _that = this;
				_that.$refs.autoform.clear('basicInfo', 'leadInventoryCounterId');
				_that.$refs.autoform.clear('basicInfo', 'inventoryDepartmentId');
			},
			onselectAssetCategory(data){
				let _that = this;
				_that.om.request(
					'assetCategoryAttribute/getOne',
					{
						id: data.id
					}
				,'post').then(res=>{
					_that.$refs.autoform.formdata.assetManagerId = res.value.userId
					_that.$refs.autoform.formdata.assetManagerName = res.value.userName
				})
			},
			clearAssetCategory(){
				let _that = this;
				_that.$refs.autoform.clear('basicInfo', 'assetCategoryId');
				_that.$refs.autoform.clear('basicInfo', 'assetManagerId');
			},
			getWorkParam() {
				let _that = this
				return {
					inventoryTakerId:_that.$refs.autoform.formdata.inventoryTakerId,
					inventoryTakerName:_that.$refs.autoform.formdata.inventoryTakerName,
				}
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.sysdictComboboxUrl("39143"),
					urlUtil.dictZeroComboboxUrl('36924'),
					{
						url: "wxPosBusiAuth/isnewList",
						param: {
							code: options.code
						},
						method: "post"
					},
				]
				//表单修改
				if (fileId) {
					ajaxArr.push({
						url: "commonForm/getInfo",
						param: {
							fileId: fileId,
							formCode: options.code
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor('basicInfo', 'assetCategoryId', 'data', configSelectData(res[0], 'id','dicname'), mainData);
					changeEditor("basicInfo","leadInventoryCounterId",{
						"link":'../../public/searchUser/searchUser',
						"linkParam":{
							other:JSON.stringify({
							}),
							isSingle:true,
							isUserPos:true,
							cachename:_that.leadInventoryCounterIdCache
						}
					},null,mainData)
					changeEditor("basicInfo","inventoryTakerId",{
						"link":'../../public/searchUser/searchUser',
						"linkParam":{
							other:JSON.stringify({
							}),
							isSingle:true,
							isUserPos:true,
							cachename:_that.inventoryTakerIdCache
						}
					},null,mainData)
					
					if (_that.subEditorObj["dataStr"]) {
						let att = {
							getWorkParam: "getWorkParam"
						}
						let configData = _that.subConfigData['dataStr']
						if (hasEditor("dataStr", "assetId", configData)) {
							let title = getEditorTitle("dataStr", 'assetId', configData)
							att.titleTemplate = "<%= assetCode%>"
						}
						if (hasEditor("dataStr", "assetName", configData)) {
							let title = getEditorTitle("dataStr", 'assetName', configData)
							att.subtitle1Template = title + ":<%= assetName%>"
						}
						changeEditor('dataStr', 'dataStr', att, mainData)
					}
					
					//子表
					if (fileId) {
						var fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[3].value) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[3].value[index])
								_that.$set(_that.formdata, index, res[3].value[index])
							}
						}
						if(_that.subEditorObj["dataStr"]){
							let dataList = _that._.cloneDeep(res[3].value.dataList)
							_that.subEditorObj['dataStr'].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'dataStr', JSON.stringify(
								dataList))
							_that.$set(_that.formdata, 'dataStr', JSON.stringify(dataList))
							delete res[3].value.dataList
						}
					}
					_that.$nextTick(function() {
						if (!_that.$refs.autoform.formdata.leadInventoryCounterId) {
							_that.$set(_that.$refs.autoform.formdata, 'leadInventoryCounterId', _that.userId)
							_that.$set(_that.$refs.autoform.formdata, 'leadInventoryCounterName', _that.userName)
							_that.$set(_that.$refs.autoform.formdata, 'inventoryDepartmentId', _that.salaryInstId)
							_that.$set(_that.$refs.autoform.formdata, 'inventoryDepartmentName', _that.salaryInstName)
							// _that.$refs.autoform.formdata.leadInventoryCounterId = _that.userId
							// _that.$refs.autoform.formdata.leadInventoryCounterName = _that.userName
							// _that.$refs.autoform.formdata.inventoryDepartmentId = _that.salaryInstId
							// _that.$refs.autoform.formdata.inventoryDepartmentName = _that.salaryInstName
						}
						if(!_that.$refs.autoform.formdata.inventoryDate){
							_that.$set(_that.$refs.autoform.formdata, 'inventoryDate', moment().format('YYYY-MM-DD'))
							// _that.$refs.autoform.formdata.inventoryDate = moment().format('YYYY-MM-DD')
						}
						if(!_that.$refs.autoform.formdata.inventoryTakerId){
							_that.$set(_that.$refs.autoform.formdata, 'inventoryTakerId', _that.userId)
							_that.$set(_that.$refs.autoform.formdata, 'inventoryTakerName', _that.userName)
							// _that.$refs.autoform.formdata.inventoryTakerId = _that.userId
							// _that.$refs.autoform.formdata.inventoryTakerName = _that.userName
						}
						_that.$refs.autoform.initSelectIndex('basicInfo', 'assetCategoryId')
						_that.showBtn(options)
					})
				}, options)
			},
			formSubmit() {
				let _that = this;
				_that.formdata.updateBottomFileFlag = true
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				if (Object.prototype.toString.call(_that.$refs.autoform.formdata.bottomFileList) == "[object String]") {
					_that.formdata.bottomFileListStr = _that.$refs.autoform.formdata.bottomFileList || ''
				} else {
					_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList ||
					[]) || ''
				}
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				let length = _that.subEditorObj["dataStr"].workList.length
				if (_that.formdata.dataStr) {
					let dataList = JSON.parse(_that.formdata.dataStr)
					for (let index in dataList) {
						if (!dataList[index].fileList) {
							delete dataList[index].fileList
						}
					}
					_that.formdata.dataStr = JSON.stringify(dataList)
				}

				if(!_that.edit){
					_that.om.formSave("deviceStocktaking/doAdd",_that.formdata,_that)
				}else{
					_that.om.formSave("deviceStocktaking/doUpdate",_that.formdata,_that)
				}
			},
		},
		onUnload() {
			let _that = this
			//#ifdef APP-PLUS
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onLoad(options) {
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.leadInventoryCounterIdCache,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata

							formdata.leadInventoryCounterId = d.id
							formdata.leadInventoryCounterName = d.username
							formdata.inventoryDepartmentId = d.instId
							formdata.inventoryDepartmentName = d.instName

							uni.removeStorage({
								key:_that.leadInventoryCounterIdCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.inventoryTakerIdCache,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.inventoryTakerId = d.id
							formdata.inventoryTakerName = d.username
							if(_that.subEditorObj['dataStr']){
								let dataList = _that.subEditorObj['dataStr'].workList
								for(let index in dataList){
									dataList[index].inventoryTakerId = d.id
									dataList[index].inventoryTakerName = d.username
								}
								formdata.dataStr = JSON.stringify(dataList)
								if(_that.subEditorObj['dataStr']){
									_that.subEditorObj['dataStr'].workList = dataList
								}
								if(dataList.length > 0){
									_that.om.toast('子表数据已更新')
								}
							}
							uni.removeStorage({
								key:_that.inventoryTakerIdCache
							})
						}
					}
				})
				uni.getStorage({
					key: "deviceStocktakingTemplateData",
					success: (res) => {
						if (res.data) {
							let d = res.data
							_that.subEditorObj["dataStr"].workList= res.data
							uni.removeStorage({
								key: "deviceStocktakingTemplateData"
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>                                                